package com.hb.trade.api.push.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.project.calibration.dto.BidWinResultDTO;
import com.hb.core.pojo.project.deal.dto.BidCandidateDTO;
import com.hb.core.pojo.project.deal.dto.BidCandidateSupplierDTO;
import com.hb.core.pojo.project.notice.dto.NoticeDTO;
import com.hb.core.pojo.project.proj.entity.AbstractProject;
import com.hb.core.pojo.project.push.entity.PushDockingLog;
import com.hb.core.util.CronExpressionUtil;
import com.hb.trade.api.push.manger.PushDockingLogManger;
import com.hb.trade.api.push.task.tenderbulletinphase.TenderBulletinPhase;
import com.hb.trade.api.push.task.tenderprojectphase.TenderProjectPhase;
import com.hb.trade.api.push.task.winbidbulletinphase.WinBidBulletinPhase;
import com.hb.trade.api.push.task.wincandidatebulletinphase.WinCandidateBulletinPhase;
import com.hb.trade.api.push.util.ConstantUtil;
import com.hb.trade.api.push.util.PushTypeEnum;
import com.hb.trade.api.push.util.PushUtil;
import com.hb.trade.client.project.*;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.msg.Message;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * describe:
 *  推送中国招投标服务的定时类
 * @author zhongdl
 * @date 2022/02/17
 */
@Component
@Slf4j
public class PushChinaBiddingTask {

    protected final ProjectClient projectClient;
    protected final PushClient pushClient;
    protected final NoticeClient noticeClient;
    protected final DealClient dealClient;
    protected final BidWinResultClient bidWinResultClient;

    public PushChinaBiddingTask(ProjectClient projectClient,
                                PushClient pushClient,
                                NoticeClient noticeClient,
                                DealClient dealClient,
                                BidWinResultClient bidWinResultClient) {
        this.projectClient = projectClient;
        this.pushClient = pushClient;
        this.noticeClient = noticeClient;
        this.dealClient = dealClient;
        this.bidWinResultClient = bidWinResultClient;
    }


    @Scheduled(cron = CronExpressionUtil.WHOLE_ONE)
    public void tenderProjectPhase(){
        log.info(">>>推送招标项目开始");
        //查询未推送的项目
        Message<List<AbstractProject>> listMessage = projectClient.listProjectAllFieldsDTO(JSONUtil.createObj());
        if (listMessage.isFail() || CollUtil.isEmpty(listMessage.getData())){
            return;
        }
        List<AbstractProject> data = listMessage.getData();
        for (AbstractProject abstractProject : data) {
            TenderProjectPhase tenderProjectPhase = new TenderProjectPhase(abstractProject);
            String xmlContent = PushUtil.replacePushData(tenderProjectPhase, PushTypeEnum.TENDER_PROJECT_PHASE);
            JSONObject jsonObject = PushUtil.callService(
                    ConstantUtil.URL,
                    ConstantUtil.SOURCE_SYSTEM_ID,
                    PushUtil.getInstanceId(),//交易流水号
                    ConstantUtil.TENDER_PROJECT_PHASE,
                    Base64Utils.encodeToString(xmlContent.getBytes(StandardCharsets.UTF_8)),
                    abstractProject.getUserId() + "",
                    StrUtil.isEmpty(abstractProject.getCreateName()) ? "无" : abstractProject.getCreateName());
            PushDockingLog pushDockingLog = PushDockingLogManger.generatePushDockingLog(PushTypeEnum.TENDER_PROJECT_PHASE, abstractProject.getId(), jsonObject);
            pushDockingLog.setPushContent(xmlContent);
            Message<Boolean> save = pushClient.save(pushDockingLog);

        }

    }
    /**
     *
     * @Description: 推送招标公告开始
     * @Author: zhongdl
     * @Date:2022/2/23 9:04
     **/
    @Scheduled(cron = CronExpressionUtil.WHOLE_ONE)
    public void tenderBulletinPhase() throws SftpException, IOException {
        log.info(">>>推送招标公告开始");
        //查询未推送的并且需要推送的招标公告
        Message<List<NoticeDTO>> listMessage = noticeClient.listPushNotice(new NoticeDTO());
        if (listMessage.isFail() || CollUtil.isEmpty(listMessage.getData())){
            return;
        }
        List<NoticeDTO> data = listMessage.getData();
        for (NoticeDTO noticeDTO : data) {
            //组装数据
            TenderBulletinPhase tenderBulletinPhase = new TenderBulletinPhase(noticeDTO);

            String xmlContent = PushUtil.replacePushData(tenderBulletinPhase, PushTypeEnum.TENDER_BULLETIN_PHASE);
            JSONObject jsonObject = PushUtil.callService(
                    ConstantUtil.URL,
                    ConstantUtil.SOURCE_SYSTEM_ID,
                    PushUtil.getInstanceId(),//交易流水号
                    ConstantUtil.TENDER_BULLETIN_PHASE,
                    Base64Utils.encodeToString(xmlContent.getBytes(StandardCharsets.UTF_8)),
                    noticeDTO.getCreateId() + "",
                    StrUtil.isEmpty(noticeDTO.getCreateName()) ? "无" : noticeDTO.getCreateName());
            PushDockingLog pushDockingLog = PushDockingLogManger.generatePushDockingLog(PushTypeEnum.TENDER_BULLETIN_PHASE, noticeDTO.getId(), jsonObject);
            pushDockingLog.setPushContent(xmlContent);

            Message<Boolean> save = pushClient.save(pushDockingLog);

        }

    }
    /**
     *
     * @Description: 推送中标候选人公示
     * @Author: zhongdl
     * @Date:2022/2/23 16:14
     **/
    @Scheduled(cron = CronExpressionUtil.WHOLE_ONE)
    public void winCandidateBulletinPhase() throws SftpException, IOException {
        log.info(">>>推送中标候选人公示");
        //查询未推送的并且需要推送的招标公告
        Message<List<BidCandidateDTO>> listMessage = dealClient.listPushBidCandidateDTO(new BidCandidateSupplierDTO());
        if (listMessage.isFail() || CollUtil.isEmpty(listMessage.getData())){
            return;
        }
        List<BidCandidateDTO> data = listMessage.getData();
        for (BidCandidateDTO bidCandidateDTO : data) {
            //组装数据
            WinCandidateBulletinPhase winCandidateBulletinPhase = new WinCandidateBulletinPhase(bidCandidateDTO);

            String xmlContent = PushUtil.replacePushData(winCandidateBulletinPhase, PushTypeEnum.WIN_CANDIDATE_BULLETIN_PHASE);
            JSONObject jsonObject = PushUtil.callService(
                    ConstantUtil.URL,
                    ConstantUtil.SOURCE_SYSTEM_ID,
                    PushUtil.getInstanceId(),//交易流水号
                    ConstantUtil.WIN_CANDIDATE_BULLETIN_PHASE,
                    Base64Utils.encodeToString(xmlContent.getBytes(StandardCharsets.UTF_8)),
                    bidCandidateDTO.getCreateId() + "",
                    StrUtil.isEmpty(bidCandidateDTO.getCreateName()) ? "无" : bidCandidateDTO.getCreateName());
            PushDockingLog pushDockingLog = PushDockingLogManger.generatePushDockingLog(PushTypeEnum.WIN_CANDIDATE_BULLETIN_PHASE, bidCandidateDTO.getId(), jsonObject);
            pushDockingLog.setPushContent(xmlContent);
            Message<Boolean> save = pushClient.save(pushDockingLog);

        }

    }


    @Scheduled(cron = CronExpressionUtil.WHOLE_ONE)
    public void winBidBulletinPhase() throws SftpException, IOException {
        log.info(">>>推送中标结果公示");
        //查询未推送的并且需要推送的招标公告
        Message<List<BidWinResultDTO>> listMessage = bidWinResultClient.listPushBidWinResultSupply(new BidWinResultDTO());
        if (listMessage.isFail() || CollUtil.isEmpty(listMessage.getData())){
            return;
        }
        List<BidWinResultDTO> data = listMessage.getData();
        for (BidWinResultDTO bidWinResultDTO : data) {
            //组装数据
            WinBidBulletinPhase winBidBulletinPhase = new WinBidBulletinPhase(bidWinResultDTO);

            String xmlContent = PushUtil.replacePushData(winBidBulletinPhase, PushTypeEnum.WIN_BID_BULLETIN_PHASE);
            JSONObject jsonObject = PushUtil.callService(
                    ConstantUtil.URL,
                    ConstantUtil.SOURCE_SYSTEM_ID,
                    PushUtil.getInstanceId(),//交易流水号
                    ConstantUtil.WIN_BID_BULLETIN_PHASE,
                    Base64Utils.encodeToString(xmlContent.getBytes(StandardCharsets.UTF_8)),
                    bidWinResultDTO.getCreateId() + "",
                    StrUtil.isEmpty(bidWinResultDTO.getCreateName()) ? "无" : bidWinResultDTO.getCreateName());
            PushDockingLog pushDockingLog = PushDockingLogManger.generatePushDockingLog(PushTypeEnum.WIN_BID_BULLETIN_PHASE, bidWinResultDTO.getId(), jsonObject);
            pushDockingLog.setPushContent(xmlContent);

            Message<Boolean> save = pushClient.save(pushDockingLog);

        }

    }
}
